import { queryInstOrderInfo } from '../../../services/inspect-project'
import { getFileJson, getFileZip } from '../../../services/report'
import { queryOrderById } from '../../../services/order'
import { queryVehicleDetail } from '../../../services/vehicle'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId:"",
    orderProjectList:[],
    ratedEnergy:'',
    batterytype:"",
    plateNo:"",
    vin:"",
    appearanceScore:"",
    insulationScore:"",
    chargingScore:"",
    disChargingScore:"",
    evalConclusionRecords1:[],
    evalConclusionRecords2:[],
    evalConclusionRecords3:[],
    evalConclusionRecords4:[],

    tableList1:[
      {
        label:"电池系统外观检查",
        value:100
      },
      {
        label:"标识及线束外观检查",
        value:100
      },
      {
        label:"总分",
        value:100
      },
    ],
    tableList2:[
      {
        label:"直流充电口",
        value:100
      },
      {
        label:"交流充电口",
        value:100
      },
      {
        label:"总分",
        value:100
      },
    ],
    tableList3:[
      {
        label:"电池健康度",
        value: 100
      },
      {
        label:"BMS精度",
        value:100
      },
      {
        label:"异常告警",
        value:100
      },
      {
        label:"温度一致性",
        value:100
      },
      {
        label:"电压一致性",
        value:100
      },
      {
        label:"SOC一致性",
        value:100
      },
      {
        label:"总分",
        value:100
      },
    ],
    tableList4:[
      {
        label:"电池健康度",
        value: 100
      },
      {
        label:"BMS精度",
        value:100
      },
      {
        label:"异常告警",
        value:100
      },
      {
        label:"SOC一致性",
        value:100
      },
      {
        label:"总分",
        value:100
      },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(!options){
      return
    }
    let orderId = options.orderId
    console.log("orderId>>>", orderId)

    this.setData({
      orderId
    })

    if(orderId){
      this.queryOrderDetailInfo(orderId)
      this.getOrderInstList(orderId)
    }
  },

  async queryOrderDetailInfo(orderId){
    let res = await queryOrderById(orderId)
    console.log("res>>>", res)
    let vehicleId = res.data.vehicleId
    this.getVehicleDetailInfo(vehicleId)
  },

  async getVehicleDetailInfo(vehicleId){
    let res = await queryVehicleDetail(vehicleId)
    let plateNo = res.data.plateNo
    let vin = res.data.vin

    this.setData({
      plateNo,
      vin
    })

  },

  async getOrderInstList(orderId){
    let res = await queryInstOrderInfo({ orderId })
    if(res.code === 200){
      let orderProjectList = res.rows
      console.log("orderProjectList>>>", orderProjectList)
      this.setData({
        orderProjectList
      })
      
      // 外观
      let selectItem1 = orderProjectList.find(item => item.type == 1)
      let itemJson1 = selectItem1.itemJson
      this.handleDealAppearance(itemJson1)

      // 绝缘
      let selectItem2 = orderProjectList.find(item => item.type == 2)
      let itemJson2 = selectItem2.itemJson
      this.handleDealInsulation(itemJson2)

      // 充电
      let selectItem3 = orderProjectList.find(item => item.type == 3)
      let itemJson3 = selectItem3.url
      this.handleDealCharging(itemJson3)

      //放电
      let selectItem4 = orderProjectList.find(item => item.type == 4)
      let itemJson4 = selectItem4.url
      this.handleDealDisCharging(itemJson4)

    }
  },

  async getFileData(jsonUrl){
    let fileInfo = {}
    fileInfo = await getFileJson(jsonUrl)

    // this.setData({
    //   reportInfo: res
    // })
  },

  async handleGetFileZip(url){
    let fileInfo = {}
    let res = await getFileZip({ url })
    if(res.code == 200){
      fileInfo = JSON.parse(res.msg)
      // this.setData({
      //   reportInfo: data
      // })
    }
    return fileInfo
  },

  // 外观
  handleDealAppearance(itemJson){
    let info = JSON.parse(itemJson)

    let appearanceScore = 100
    let score1 = 100
    let score2 = 100
    let scoreSource1 = JSON.parse(info[1]['itemJson'])
    let scoreSource2 = JSON.parse(info[2]['itemJson'])

    let faultNum1 = 0
    let faultNum2 = 0
    
    scoreSource1.forEach(item => {
      if(item.checkRes && item.checkRes != '1'){
        faultNum1++
      }
    })

    scoreSource2.forEach(item => {
      if(item.checkRes && item.checkRes != '1'){
        faultNum2++
      }
    })

    score1 = score1 - (faultNum1 * 10)
    score2 = score2 - (faultNum2 * 20)
    appearanceScore = (Number(score1) + Number(score2)) / 2

    let tableList1 = [
      {
        label:"电池系统外观检查",
        value: score1 || 100
      },
      {
        label:"标识及线束外观检查",
        value: score2 || 100
      },
      {
        label:"总分",
        value: appearanceScore || 100
      },
    ]
    let evalConclusionRecords1 = []

    let tempEvalConclusionRecords1 = ['车辆的外观状况极为出色，显示出高度的维护标准。漆面光洁，无明显划痕或凹陷，所有的装饰件和外部附件均完好无损，符合甚至超过了制造商的原厂标准。']
    let tempEvalConclusionRecords2 = ['车辆的外观整洁，基本维护得当。可能存在轻微的磨损或小划痕，但不影响车辆的整体美观。推荐定期做漆面保养和小修小补，以保持车辆的良好外观。']
    let tempEvalConclusionRecords3 = ['车辆外观存在一些较为明显的划痕或损伤，可能需要进行细致的修复工作。建议进行专业的外观修复和定期保养，以防止损伤进一步恶化，并提升车辆的市场价值。']

    if(appearanceScore > 89){
      evalConclusionRecords1 = tempEvalConclusionRecords1
    } else if(appearanceScore > 69 && appearanceScore < 90){
      evalConclusionRecords1 = tempEvalConclusionRecords2
    } else {
      evalConclusionRecords1 = tempEvalConclusionRecords3
    }

    this.setData({
      evalConclusionRecords1,
      appearanceScore,
      tableList1
    })

    this.handleCalculateTotalScore()
  },

  // 绝缘
  handleDealInsulation(itemJson){
    let info = JSON.parse(itemJson)

    console.log("info>>>", info)

    let insulationScore = 100
    let score1 = 100
    let score2 = 100
    let scoreSource1 = info[0]['infoList']
    let scoreSource2 = info[1]['infoList']

    let faultNum1 = 0
    let faultNum2 = 0
    
    scoreSource1.forEach(item => {
      if(item.checkRes && item.checkRes == '不合格'){
        faultNum1++
      }
    })

    scoreSource2.forEach(item => {
      if(item.checkRes && item.checkRes == '不合格'){
        faultNum2++
      }
    })

    score1 = score1 - (faultNum1 * 30)
    score2 = score2 - (faultNum2 * 30)
    insulationScore = (Number(score1) + Number(score2)) / 2

    let tableList2 = [
      {
        label:"直流充电口",
        value: score1 || 100
      },
      {
        label:"交流充电口",
        value: score2 || 100
      },
      {
        label:"总分",
        value: insulationScore
      },
    ]

    let evalConclusionRecords2 = []

    let tempEvalConclusionRecords1 = ['车辆的绝缘性能表现优异，电气系统的安全性高，有效降低了漏电的风险，提供了良好的驾驶安全保障。']
    let tempEvalConclusionRecords2 = ['车辆的绝缘性能整体符合标准，但在某些高负荷条件下可能会有轻微的性能下降。建议监控电气系统的绝缘性能，并进行定期的维护。']
    let tempEvalConclusionRecords3 = ['车辆的绝缘检测分数指出了安全隐患，必须优先处理。建议立即进行详细的电气系统检查，并修复所有已识别的绝缘问题，以确保安全。']

    if(insulationScore > 89){
      evalConclusionRecords2 = tempEvalConclusionRecords1
    } else if(insulationScore > 69 && insulationScore < 90){
      evalConclusionRecords2 = tempEvalConclusionRecords2
    } else {
      evalConclusionRecords2 = tempEvalConclusionRecords3
    }


    this.setData({
      evalConclusionRecords2,
      insulationScore,
      tableList2
    })
    this.handleCalculateTotalScore()
  },

  // 充电
  async handleDealCharging(jsonUrl){
    let fileInfo = {}
    if(jsonUrl.indexOf('.json') > -1){
      fileInfo = await this.getFileData(jsonUrl)
    } else {
      fileInfo = await this.handleGetFileZip(jsonUrl)
    }

    console.log("充电fileInfo>>>", fileInfo)
    //标称总能量 ratedEnergy
    //电池类型 batterytype
    let ratedEnergy = fileInfo.ratedEnergy
    let batterytype = fileInfo.batterytype
    let chargingScore = fileInfo.score || 100

    let evalConclusionRecords3 = fileInfo.evalConclusionRecords //评估结论

    let sohValue = fileInfo.sohValue || 100 //电池健康度
    let bmsAccuracy = fileInfo.bmsAccuracy || 100 //BMS精度
    let alarmScore = fileInfo.alarmScore || 100 //异常告警
    let uniformityTmp = fileInfo.uniformityTmp || 100 //温度一致性
    let uniformityV = fileInfo.uniformityV || 100 //电压一致性
    let socAccuracy = fileInfo.socAccuracy || 100 //SOC一致性

    let tableList3 = [
      {
        label:"电池健康度",
        value: sohValue
      },
      {
        label:"BMS精度",
        value:bmsAccuracy
      },
      {
        label:"异常告警",
        value:alarmScore
      },
      {
        label:"温度一致性",
        value:uniformityTmp
      },
      {
        label:"电压一致性",
        value:uniformityV
      },
      {
        label:"SOC一致性",
        value:socAccuracy
      },
      {
        label:"总分",
        value:chargingScore
      },
    ]

    this.setData({
      ratedEnergy,
      batterytype,
      chargingScore,
      evalConclusionRecords3,
      tableList3
    })
    this.handleCalculateTotalScore()
  },

  //放电
  async handleDealDisCharging(jsonUrl){
    let fileInfo = {}
    if(jsonUrl.indexOf('.json') > -1){
      fileInfo = await this.getFileData(jsonUrl)
    } else {
      fileInfo = await this.handleGetFileZip(jsonUrl)
    }
    console.log("放电信息>>>>", fileInfo)

    let disChargingScore = fileInfo.score || 100
    let evalConclusionRecords4 = fileInfo.evalConclusionRecords //评估结论

    let sohValue = fileInfo.sohValue || 100 //电池健康度
    let bmsAccuracy = fileInfo.bmsAccuracy || 100 //BMS精度
    let alarmScore = fileInfo.alarmScore || 100 //异常告警
    let socAccuracy = fileInfo.soclinearitynow || 100 //SOC一致性

    let tableList4 = [
      {
        label:"电池健康度",
        value: sohValue
      },
      {
        label:"BMS精度",
        value:bmsAccuracy
      },
      {
        label:"异常告警",
        value:alarmScore
      },
      {
        label:"SOC一致性",
        value:socAccuracy
      },
      {
        label:"总分",
        value:disChargingScore
      },
    ]


    this.setData({
      disChargingScore,
      evalConclusionRecords4,
      tableList4
    })
    this.handleCalculateTotalScore()
  },

  handleCalculateTotalScore(){
    let appearanceScore = this.data.appearanceScore || 100
    let insulationScore = this.data.insulationScore || 100
    let chargingScore = this.data.chargingScore || 100
    let disChargingScore = this.data.disChargingScore || 100

    let totalScore = Math.floor ((Number(appearanceScore) + Number(insulationScore) + Number(chargingScore) + Number(disChargingScore)) / 4)  

    this.setData({
      totalScore
    })


  }


})